package com.buddy.learn.algorithm.simple;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Buddy
 * @date 2021/4/21
 */
public class TowSum {

    public static void main(String[] args) {
        int[] arr = {2, 6, 11, 15};
        int target = 26;
        int[] result = getSumIndex(arr, target);
        System.out.println(Arrays.toString(result));

    }

    private static int[] getSumIndex(int[] arr, int target) {
        Map<Integer, Integer> cache = new HashMap<>(4);
        for (int i = 0, length = arr.length; i < length; i++) {
            int diff = target - arr[i];
            if (cache.containsKey(diff)) {
                return new int[]{cache.get(diff), i};
            }
            cache.put(arr[i],i);
        }
        return new int[]{-1,-1};
    }
}
